/*
 * Copyright (c) 2023-2025 elsfs Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.elsfs.cloud.common.mybatis.ext.mybatis.extension;

import com.baomidou.mybatisplus.core.conditions.SharedString;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.elsfs.cloud.common.mybatis.ext.sql.CompositeSqlFragment;
import org.elsfs.cloud.common.mybatis.ext.sql.JoinType;
import org.elsfs.cloud.common.mybatis.ext.sql.TableAliasManager;
import org.elsfs.cloud.common.util.lang.ArrayUtils;
import org.elsfs.cloud.common.util.lang.CollectionUtils;

/**
 * Join查询包装器
 *
 * @param <E> 实体类型
 */
public class JoinQueryWrapper<E>
    extends AbstractJoinWrapper<
        E,
        String,
        JoinQueryWrapper<E>,
        WrapperSelectJoinBuilderImpl<E, String, JoinQueryWrapper<E>>,
        WrapperSelectJoinBuilderImpl<E, String, JoinQueryWrapper<E>>
            .WrapperJoinConditionBuilderImpl> {

  public JoinQueryWrapper(E entity) {
    super(entity);
  }

  public JoinQueryWrapper(Class<E> entityClass) {
    super(entityClass);
  }

  public JoinQueryWrapper(Class<E> entityClass, TableAliasManager tableAliasManager) {
    super(entityClass, tableAliasManager);
  }

  /**
   * 非对外公开的构造方法,只用于生产嵌套 sql
   *
   * @param entityClass 本不应该需要的
   */
  private JoinQueryWrapper(
      E entity,
      Class<E> entityClass,
      AtomicInteger paramNameSeq,
      Map<String, Object> paramNameValuePairs,
      MergeSegments mergeSegments,
      SharedString lastSql,
      SharedString sqlComment,
      SharedString sqlFirst,
      TableAliasManager tableAliasManager) {
    super(
        entity,
        entityClass,
        paramNameSeq,
        paramNameValuePairs,
        mergeSegments,
        lastSql,
        sqlComment,
        sqlFirst,
        tableAliasManager);
  }

  /** 子类返回一个自己的新对象 */
  @Override
  protected JoinQueryWrapper<E> instance() {
    return new JoinQueryWrapper<>(
        this.getEntity(),
        this.getEntityClass(),
        this.paramNameSeq,
        this.paramNameValuePairs,
        new MergeSegments(),
        SharedString.emptyString(),
        SharedString.emptyString(),
        SharedString.emptyString(),
        this.tableAliasManager);
  }

  /**
   * 自定义选择字段
   *
   * @param fields 字段列表
   * @return this
   */
  @Override
  public JoinQueryWrapper<E> select(String... fields) {
    if (ArrayUtils.isNotEmpty(fields)) {
      this.selectedFields.setStringValue(
          JoinWrapper.mergeSelectedFields(this.selectedFields.getStringValue(), fields));
    }
    return this;
  }

  /**
   * 指定查询字段
   *
   * @param condition 执行条件
   * @param columns 字段列表
   * @return children
   */
  @Override
  public JoinQueryWrapper<E> select(boolean condition, List<String> columns) {
    // todo 待验证
    if (CollectionUtils.isNotEmpty(columns)) {
      this.selectedFields.setStringValue(
          JoinWrapper.mergeSelectedFields(this.selectedFields.getStringValue(), columns));
    }
    return this;
  }

  /**
   * 自定义选择字段
   *
   * @param fields 字段集合
   * @return this
   */
  public JoinQueryWrapper<E> select(Collection<String> fields) {
    if (CollectionUtils.isNotEmpty(fields)) {
      this.selectedFields.setStringValue(
          JoinWrapper.mergeSelectedFields(this.selectedFields.getStringValue(), fields));
    }

    return this;
  }

  /**
   * 构建SQL片段
   *
   * @return SQL片段
   */
  @Override
  public String buildSqlFragment() {
    if (this.joinBuilders.isEmpty()) {
      return "";
    }
    return this.joinBuilders.stream()
        .map(CompositeSqlFragment::buildSqlFragment)
        .collect(Collectors.joining(" "));
  }

  @Override
  protected WrapperSelectJoinBuilderImpl<E, String, JoinQueryWrapper<E>> getNewJoinBuilder(
      JoinQueryWrapper<E> joinQueryWrapper, JoinType joinType, String table) {
    return new WrapperSelectJoinBuilderImpl<>(joinQueryWrapper, joinType, table);
  }

  @Override
  protected WrapperSelectJoinBuilderImpl<E, String, JoinQueryWrapper<E>> getNewJoinBuilder(
      JoinQueryWrapper<E> joinQueryWrapper,
      JoinType joinType,
      String table,
      String tableAlias,
      boolean registerAlias) {
    return new WrapperSelectJoinBuilderImpl<>(
        joinQueryWrapper, joinType, table, tableAlias, registerAlias);
  }

  @Override
  protected WrapperSelectJoinBuilderImpl<E, String, JoinQueryWrapper<E>> getNewJoinBuilder(
      JoinQueryWrapper<E> joinQueryWrapper, JoinType joinType, String table, String tableAlias) {
    return new WrapperSelectJoinBuilderImpl<>(joinQueryWrapper, joinType, table, tableAlias);
  }
}
